package com.ran.impl;

import com.ran.dao.BaseDAO;
import com.ran.dao.BookDao;
import com.ran.po.Book;

import java.util.List;


public class BookDaoImpl extends BaseDAO<Book> implements BookDao {
    @Override
    public List<Book> getAll() {
        String sql = "select number,name,author from t_book";
        return getForList(Book.class,sql);
    }

    @Override
    public Book getById(String number) {
        String sql = "select number,name,author from t_book where number=?";
        return getInstance(Book.class,sql,number);
    }

    @Override
    public int add(Book book) {
        String sql = "insert into t_book(number,name,author) values(?,?,?)";
        return update(sql,book.getNumber(),book.getName(),book.getAuthor());
    }

    @Override
    public int delete(String number) {
        String sql = "delete from t_book where number =?";
        return update(sql,number);
    }

    @Override
    public int update(Book book) {
        String sql = "update t_book set name=?,author=? where number=?";
        return update(sql,book.getName(),book.getAuthor(),book.getNumber());
    }

    public static void main(String[] args) {
        BookDao bookDao = new BookDaoImpl();
        for (Book book: bookDao.getAll()
             ) {
            System.out.println(book.getName());
        }
    }


}
